package com.sys.util;

import java.nio.ByteBuffer;

@SuppressWarnings("unused")
public class byteUtils {

    /**
     * 通过byte数组取到uint8
     *
     * @param b 字节数组
     * @return 第几位开始取
     */
    public static short getUInt8(byte b) {
        short ret;
        if (b > 0) {
            ret = (short) b;
        } else {
            ret = (short) (b + 256);
        }
        return ret;
    }

    /**
     * 通过byte数组取到int16
     *
     * @param b     字节数组
     * @param index 第几位开始取
     * @return short
     */
    public static short getInt16(byte[] b, int index) {
        return (short) (((b[index + 1] << 8) | b[index] & 0xff));
    }

    /**
     * 通过byte数组取到uint16
     *
     * @param b     字节数组
     * @param index 第几位开始
     * @return uint16
     */
    public static int getUInt16(byte[] b, int index) {
        return (((b[index + 1] & 0xff << 8) | b[index] & 0xff));
    }

    /**
     * 通过byte数组取到int32
     *
     * @param bb    字节数组
     * @param index 第几位开始
     * @return int
     */
    public static int getInt32(byte[] bb, int index) {
        return (int) ((((bb[index + 3] & 0xff) << 24)
                | ((bb[index + 2] & 0xff) << 16)
                | ((bb[index + 1] & 0xff) << 8) | ((bb[index] & 0xff))));
    }

    /**
     * 通过byte数组取到uint32
     *
     * @param bb    字节数组
     * @param index 第几位开始
     * @return long
     */
    public static long getUInt32(byte[] bb, int index) {
        return ((((long) bb[index + 3] & 0xff) << 24)
                | (((long) bb[index + 2] & 0xff) << 16)
                | (((long) bb[index + 1] & 0xff) << 8) | (((long) bb[index] & 0xff)));
    }

    /**
     * 通过byte数组取到int64
     *
     * @param bb    字节数组
     * @param index 第几位开始
     * @return long
     */
    public static long getInt64(byte[] bb, int index) {
        return ((((long) bb[index + 7] & 0xff) << 56)
                | (((long) bb[index + 6] & 0xff) << 48)
                | (((long) bb[index + 5] & 0xff) << 40)
                | (((long) bb[index + 4] & 0xff) << 32)
                | (((long) bb[index + 3] & 0xff) << 24)
                | (((long) bb[index + 2] & 0xff) << 16)
                | (((long) bb[index + 1] & 0xff) << 8) | (((long) bb[index] & 0xff)));
    }

    /**
     * 通过byte数组取得float32
     *
     * @param b     字节数组
     * @param index 第几位开始
     * @return float
     */
    public static float getFloat32(byte[] b, int index) {
        int l;
        l = b[index];
        l &= 0xff;
        l |= ((long) b[index + 1] << 8);
        l &= 0xffff;
        l |= ((long) b[index + 2] << 16);
        l &= 0xffffff;
        l |= ((long) b[index + 3] << 24);
        return Float.intBitsToFloat(l);
    }

    /**
     * 通过byte数组取得float64
     *
     * @param b     字节数组
     * @param index 第几位开始
     * @return double
     */
    public static double getFloat64(byte[] b, int index) {
        long l;
        l = b[index];
        l &= 0xff;
        l |= ((long) b[index + 1] << 8);
        l &= 0xffff;
        l |= ((long) b[index + 2] << 16);
        l &= 0xffffff;
        l |= ((long) b[index + 3] << 24);
        l &= 0xffffffffL;
        l |= ((long) b[index + 4] << 32);
        l &= 0xffffffffffL;
        l |= ((long) b[index + 5] << 40);
        l &= 0xffffffffffffL;
        l |= ((long) b[index + 6] << 48);
        l &= 0xffffffffffffffL;
        l |= ((long) b[index + 7] << 56);
        return Double.longBitsToDouble(l);
    }

    /**
     * int转byte数组
     *
     * @param intValue 需要转的值
     * @return byte数组
     */
    public byte[] intToBytes(int intValue) {
        ByteBuffer buffer = ByteBuffer.allocate(4);
        buffer.putInt(intValue);
        return buffer.array();
    }

    /**
     * float转byte数组
     *
     * @param floatValue 需要转的值
     * @return byte数组
     */
    public byte[] floatToBytes(float floatValue) {
        ByteBuffer buffer = ByteBuffer.allocate(4);
        buffer.putFloat(floatValue);
        return buffer.array();
    }

    /**
     * long转byte数组
     *
     * @param longValue 需要转的值
     * @return byte数组
     */
    public byte[] longToBytes(long longValue) {
        ByteBuffer buffer = ByteBuffer.allocate(8);
        buffer.putLong(longValue);
        return buffer.array();
    }

    /**
     * double转byte数组
     *
     * @param doubleValue 需要转的值
     * @return byte数组
     */
    public byte[] doubleToBytes(double doubleValue) {
        ByteBuffer buffer = ByteBuffer.allocate(8);
        buffer.putDouble(0, doubleValue);
        return buffer.array();
    }

    /**
     * short转byte数组
     *
     * @param shortValue 需要转的值
     * @return byte数组
     */
    public byte[] shortToBytes(short shortValue) {
        ByteBuffer buffer = ByteBuffer.allocate(2);
        buffer.putShort(0, shortValue);
        return buffer.array();
    }
}
